using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class P_Talking : MonoBehaviour
{
    public GameObject[] ShowHodeGame;
    public Text grandpa;
    public Text smallfarmer;
    public Image GrandpaImage;
    public Image SmallImage;
    public GameObject nextButton;
    private bool isNext;
    private List<P_People> conversation;
    private void OnEnable()
    {
        Init();
        StartCoroutine(Talking());
    }
    private void OnDisable()
    {
        StopAllCoroutines();
    }
    private void Init()
    {
        conversation = new List<P_People>
        {
            new P_People(0,"有粮无粮在于水，粮多粮少在于肥。"),
            new P_People(1,"这是什么意思啊。"),
            new P_People(0,"通过浇水，大葱可以成苗。"),
            new P_People(0,"而合理施肥，可以提高大葱品质。"),
            new P_People(1,"那我们怎样施肥呢？"),
            new P_People(0,"我们这样……")
        };
    }
    private IEnumerator Talking()
    {
        foreach (var obj in conversation)
        {
            if (obj.people == 0)
            {
                GrandpaImage.gameObject.SetActive(true);
                SmallImage.gameObject.SetActive(false);
                yield return StartCoroutine(Typing(grandpa, obj.msg));
            }
            else
            {
                GrandpaImage.gameObject.SetActive(false);
                SmallImage.gameObject.SetActive(true);
                yield return StartCoroutine(Typing(smallfarmer, obj.msg));
            }
            nextButton.SetActive(true);
            while (!isNext)
            {
                yield return null;
            }
            isNext = false;
        }
        GrandpaImage.gameObject.SetActive(false);
        SmallImage.gameObject.SetActive(false);
        GameObject.FindObjectOfType<P_ButtonManager>().ShowNextButton(ShowHodeGame[1],ShowHodeGame[0]);
    }
    private IEnumerator Typing(Text text, string msg)
    { 
        for (int i = 0; i < msg.Length; i++)
        {
            text.text += msg[i];
            //yield return new WaitForSeconds(0.05f);
        }
        yield return null;
    }
    public void NextButton()
    {
        isNext = true;
        grandpa.text="";
        smallfarmer.text="";
        nextButton.SetActive(false);
    }
}
public struct P_People
{
    public int people; 
    public string msg; 
    public P_People(int p, string m)
    {
        people = p;
        msg = m;
    }
}
